package org.etom.impl.ui.wizards;

import org.eclipse.jface.action.MenuManager;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.Text;

/**
 * The abstract class initializes the GUI for the given wizard page.
 */
public class AbstractUploadWizardPage1 extends WizardPage {

  private static final String PAGE_NAME = "UploadWizardPage2"; //$NON-NLS-1$

  private Composite composite;
  protected TreeViewer treeViewer;
  protected Text fileNameText;
  protected MenuManager menuManager;

  public AbstractUploadWizardPage1() {
    super(PAGE_NAME);
  }

  @Override
  public void createControl(Composite parent) {
    initComposite(parent);
    addTreeViewer();
    addFileNameControls();
    initPage();
    createContextMenu();
  }

  private void addTreeViewer() {
    //create tree composite
    Composite treeComposite = new Composite(composite, SWT.BORDER);
    treeComposite.setLayout(new FillLayout());
    GridData gridData = new GridData();
    gridData.grabExcessHorizontalSpace = true;
    gridData.grabExcessVerticalSpace = true;
    gridData.horizontalAlignment = SWT.FILL;
    gridData.verticalAlignment = SWT.FILL;
    gridData.horizontalSpan = 2;
    treeComposite.setLayoutData(gridData);
    
    //create tree viewer
    treeViewer = new TreeViewer(treeComposite);
  }

  private void addFileNameControls() {
    //add label
    Label fileNameLabel = new Label(composite, SWT.NONE);
    fileNameLabel.setText(WizardsMessages.Upload_Wizard_Page_File_Name_Label);
    
    //add text
    fileNameText = new Text(composite, SWT.BORDER);
    GridData gridData = new GridData();
    gridData.grabExcessHorizontalSpace = true;
    gridData.horizontalAlignment = SWT.FILL;
    fileNameText.setLayoutData(gridData);
  }

  private void initComposite(Composite parent) {
    this.composite = new Composite(parent, SWT.NONE);
    this.composite.setLayout(new GridLayout(2, false));
  }

  private void initPage() {
    setControl(composite);
    setDescription(WizardsMessages.Upload_Wizard_Page1_Description);
    setTitle(WizardsMessages.Upload_Wizard_Page_Title);
  }

  /*
   * The method creates the context menu.
   */
  private void createContextMenu() {
    menuManager = new MenuManager();
    menuManager.setRemoveAllWhenShown(true);
    Menu treeContextMenu = menuManager.createContextMenu(
        this.treeViewer.getControl());
    this.treeViewer.getControl().setMenu(treeContextMenu);
  }
  
}
